#!/bin/bash

if [ $# -lt 1 ]
then
    echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]	\033[31mERROR\033[0m No Args Input..."
    exit ;
fi

function start_hadoop()
{
        echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m 启动 hadoop集群"

        echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m 启动 hdfs"
        ssh hadoop102 "/opt/module/hadoop-3.1.3/sbin/start-dfs.sh"
        echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m 启动 yarn"
        ssh hadoop103 "/opt/module/hadoop-3.1.3/sbin/start-yarn.sh"
        echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m 启动 historyserver"
        ssh hadoop102 "/opt/module/hadoop-3.1.3/bin/mapred --daemon start historyserver"
}

function stop_hadoop()
{
        echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m 关闭 hadoop集群"

        echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m 关闭 historyserver"
        ssh hadoop102 "/opt/module/hadoop-3.1.3/bin/mapred --daemon stop historyserver"
        echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m 关闭 yarn"
        ssh hadoop103 "/opt/module/hadoop-3.1.3/sbin/stop-yarn.sh"
        echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m 关闭 hdfs"
        ssh hadoop102 "/opt/module/hadoop-3.1.3/sbin/stop-dfs.sh"
}

function status_hadoop()
{
	for host in hadoop102 hadoop103 hadoop104
	do
        	echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m]		\033[32mINFO\033[0m $host"
        	ssh $host jps 
	done
}

case $1 in
"start")
start_hadoop
;;
"stop")
stop_hadoop
;;
"status")
status_hadoop
;;
"restart")
stop_hadoop
echo -e "[ \033[36m$(date "+%Y-%m-%d %H:%M:%S")\033[0m ]    \033[32mINFO\033[0m please waiting..."
sleep 3
start_hadoop
;;
*)
	echo "使用: myhadoop.sh 参数名"
        echo "命令参数列表："
        echo "start 先启动hadoop服务"
        echo "stop 关闭hadoop服务"
        echo "status 查看hadoop启动情况"
        echo "restart 重启hadoop服务"
;;
esac

